﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Deep_Talk_Converter.RenderWorkItems;

namespace Deep_Talk_Converter.RenderTasks
{
    /// <summary>
    /// Render the image locally - no need ot try to pipe it up to silverlight streaming.
    /// </summary>
    class RenderLocal : RenderBase
    {
        string _url;
        MainWindow _host;

        public RenderLocal(string url, MainWindow host)
        {
            _url = url;
            _host = host;
        }

        /// <summary>
        /// Start the task off (in the background).
        /// </summary>
        override public void Start()
        {
            ThreadPool.QueueUserWorkItem(o => RunRender(_url));
        }

        /// <summary>
        /// We actually run the render. We expect to be going on a background thread.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private void RunRender(string url)
        {
            RenderMeetingLocally m = new RenderMeetingLocally(url);
            m.MeetingNameKnown += new RenderMeetingLocally.MeetingNameCallback(m_MeetingNameKnown);
            m.ProgressMessage += new RenderMeetingLocally.ProgressMessageCallback(m_ProgressMessage);
            m.RenderDone += new RenderMeetingLocally.RenderDoneCallback(m_RenderDone);
            m.Render();
        }

        void m_RenderDone(bool ok, string message, string uri)
        {
            FireRenderDone(ok, message);
            _host.SetFinalURL(uri);
        }

        /// <summary>
        /// Callback with the progress message...
        /// </summary>
        /// <param name="message"></param>
        void m_ProgressMessage(string message)
        {
            FireProgressUpdate(message);
        }

        void m_MeetingNameKnown(string meetingName)
        {
            FireMeetingName(meetingName);
        }
    }
}
